package com.beichuan_travel.controller;

import com.beichuan_travel.enums.Sort;
import com.beichuan_travel.model.dto.FoodDTO;
import com.beichuan_travel.model.dto.RestaurantDTO;
import com.beichuan_travel.model.vo.FoodVO;
import com.beichuan_travel.model.vo.RestaurantVO;
import com.beichuan_travel.model.vo.ResultVO;
import com.beichuan_travel.service.FoodService;
import com.beichuan_travel.service.RestaurantService;
import com.beichuan_travel.utils.ResponseResult;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@Api(value = "餐馆接口", tags = "餐馆接口")
@RequestMapping("/restaurant")
public class RestaurantController {

    @Resource
    RestaurantService restaurantService;

    @Resource
    ResponseResult responseResult;


    @ApiOperation(value = "根据名字查询餐馆，如果有查询结果就直接返回对象", notes = "根据名字查询餐馆--sora")
    @ApiImplicitParam(name = "name", value = "要查询的餐馆名字", required = true, dataType = "string", paramType = "query")
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 1003, message = "查询结果为空")
    })
    @GetMapping("/v1/search/name")
    public ResultVO queryByName(@RequestParam("name") String name) {
        RestaurantVO restaurantVO = restaurantService.queryByName(name);
        if (restaurantVO == null) {
            return responseResult.empty();
        }
        return responseResult.ok(restaurantVO);
    }


    @ApiOperation(value = "根据餐馆人均价格范围查询餐馆", notes = "根据价格查询餐馆--sora")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lower", value = "最低价格", required = true, dataType = "Int", paramType = "query"),
            @ApiImplicitParam(name = "upper", value = "最高价格", required = true, dataType = "Int", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 1003, message = "查询结果为空")
    })
    @GetMapping("/v1/search/price")
    public ResultVO queryByPrice(@RequestParam("lower") Integer lower, @RequestParam("upper") Integer upper) {
        List<RestaurantVO> restaurantVOList = restaurantService.queryByPrice(lower, upper);
        if (restaurantVOList == null) {
            return responseResult.empty();
        }
        return responseResult.ok(restaurantVOList);
    }

    @ApiOperation(value = "根据星级查询，星级为小数，用户选择整数a，取值为[a,a+1]之间的美食。查询结果为列表，按星级从高到低排序", notes = "根据星级查询--sora")
    @ApiImplicitParam(name = "star", value = "用户选择的星级", required = true, dataType = "Int", paramType = "query")
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 1003, message = "查询结果为空")
    })
    @GetMapping("/v1/search/star")
    public ResultVO queryByStar(@RequestParam("star") Integer star) {
        List<RestaurantVO> restaurantVOList = restaurantService.queryByStar(star);
        if (restaurantVOList == null) {
            return responseResult.empty();
        }
        return responseResult.ok(restaurantVOList);
    }


    @ApiOperation(value = "根据名字模糊查询，并将名字升序排列，返回的是一个数组", notes = "根据名字模糊查询--sora")
    @ApiImplicitParam(name = "keyword", value = "要模糊查询的名字", required = true, dataType = "string", paramType = "query")
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 1003, message = "查询结果为空")
    })
    @GetMapping("/v1/search/name/like")
    public ResultVO fuzzyQueryName(@RequestParam("keyword") String keyword) {
        List<RestaurantVO > restaurantVOList = restaurantService.fuzzyQueryName(keyword);
        if (restaurantVOList == null) {
            return responseResult.empty();
        }
        return responseResult.ok(restaurantVOList);
    }


    @ApiOperation(value = "根据餐馆星级遍历所有餐馆，默认为desc，即从最高分开始降序排列", notes = "根据星级遍历所有餐馆--sora")
    @ApiImplicitParam(name = "type", value = "是否需要升序排列，若需要请传入0", required = false, dataType = "integer", paramType = "query")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 1002, message = "请输入数字0和1")
    })
    @GetMapping("/v1/search/all/star")
    public ResultVO queryAllByStar(Integer type){
        if (type == null) {
            type = Sort.DESC.getSort();
        }
        List<RestaurantVO> restaurantVOList = restaurantService.queryAllDescByStar(type);
        if (restaurantVOList == null) {
            return responseResult.empty();
        }
        return responseResult.ok(restaurantVOList);
    }


    @ApiOperation(value = "根据餐馆平均费用遍历所有餐馆，默认asc，即从最低价开始排序", notes = "根据价格排序--sora")
    @ApiImplicitParam(name = "type", value = "是否需要升序排列，若需要请传入0", required = false, dataType = "integer", paramType = "query")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 1002, message = "请输入数字0和1")
    })
    @GetMapping("/v1/search/all/price")
    public ResultVO queryAllByPrice(Integer type){
        if (type == null) {
            type = Sort.ASC.getSort();
        }
        List<RestaurantVO> restaurantVOList = restaurantService.queryAllASCByPrice(type);
        if (restaurantVOList == null) {
            return responseResult.empty();
        }
        return responseResult.ok(restaurantVOList);
    }

    @ApiOperation(value = "添加餐馆", notes = "添加餐馆--sora")
    @ApiResponses({
            @ApiResponse(code = 200, message = "添加成功"),
            @ApiResponse(code = 200, message = "添加失败")
    })
    @PostMapping("/v1/add")
    public ResultVO addAttraction(@Valid RestaurantDTO restaurantDTO) {
        if (restaurantService.addRestaurant(restaurantDTO) > 0) {
            return responseResult.ok();
        } else {
            return responseResult.faile();
        }
    }

    @ApiOperation(value = "查找餐馆所属食物", notes = "餐馆餐馆所属食物--sora")
    @ApiImplicitParam(name = "restaurantId", value = "要查询的餐馆id", required = true, paramType = "path", dataType = "Int")
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 1003, message = "查询结果为空")
    })
    @GetMapping("/v1/food/{restaurantId}")
    public ResultVO queryBelongFood(@PathVariable("restaurantId") Integer restaurantId) {
        List<FoodVO> voList = restaurantService.queryAllFood(restaurantId);
        if (voList == null) {
            return responseResult.empty();
        }
        return responseResult.ok(voList);
    }
}
